<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1000" destroy-on-close @closed="$emit('closed')" align-center>
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="110px" label-position="right">
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="计划名称" prop="title">
                        <el-input v-model="form.title" placeholder="请输入计划名称" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="计划编号" prop="code">
                        <el-input v-model="form.code" placeholder="计划编号" clearable autocomplete="off"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="计划类型" prop="type">
                        <el-select v-model="form.type" placeholder="请选择计划类型" style="width:100%">
                            <el-option label="周期" :value="1"></el-option>
                            <el-option label="执行间隔" :value="2"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="周期单位" prop="unit">
                        <el-select v-model="form.unit" placeholder="请选择周期单位" style="width:100%" @change="change">
                            <el-option label="小时" :value="1"></el-option>
                            <el-option label="天" :value="2"></el-option>
                            <el-option label="周" :value="3"></el-option>
                            <el-option label="月" :value="4"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="跳过时间" prop="time">
                        <el-select v-model="form.time" multiple placeholder="请选择跳过时间" style="width:100%">
                            <el-option label="周六" :value="1"></el-option>
                            <el-option label="周日" :value="2"></el-option>
                            <el-option label="法定节假日" :value="3"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="周期" prop="num">
                        <el-input v-model="form.num" placeholder="请输入周期" clearable>
                            <template #append>{{dayType}}</template>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="跳过日期" prop="date">
                        <el-input v-model="form.date" placeholder="请输入跳过日期" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="是否禁用" prop="system_disable">
                        <el-switch v-model="form.system_disable" :active-value="1" :inactive-value="0"></el-switch>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="12">
                    <el-form-item label="备注" prop="remark">
                        <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="排序" prop="sort">
                        <el-input v-model="form.sort" placeholder="排序" clearable autocomplete="off"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增计划',
				edit: '编辑计划',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id: "",
				title: "",
				code: "",
                sort:"",
                type:"",
                num:"",
                unit:"",
                time:"",
                date:"",
				remark: "",
                system_disable: 0,
			},
            dayType:"",
			//验证规则
			rules: {
				title: [
					{ required: true, message: '请输入计划名称' }
				],
				code: [
					{ required: true, message: '请输入计划编号' }
				],
                type: [
					{ required: true, message: '请选择计划类型' }
				],
                unit: [
					{ required: true, message: '请选择周期单位' }
				],
                num: [
					{ required: true, message: '请输入周期' }
				],
			},
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//加载树数据
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.device.Plan.edit.post(this.form);
					} else {
						res = await this.$API.device.Plan.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", { type: 'error' })
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.id = data.id
			this.form.title = data.title
			this.form.code = data.code
            this.form.sort = data.sort
			this.form.type = data.type
			this.form.num = data.num
			this.form.unit = data.unit
            let a = []
            data.time.map(item=>{
                a.push(Number(item))
            })
			this.form.time = a
			this.form.date = data.date
			this.form.remark = data.remark
			this.form.system_disable = data.system_disable
            this.dayType = data.unit_title
		},
        change(e){
            if(e==1){
                this.dayType = "小时"
            }else if(e==2){
                this.dayType = "天"
            }else if(e==3){
                this.dayType = "周"
            }else{
                this.dayType = "月"
            }
        },
	}
}
</script>

<style></style>
